package com.example.jh352160.surfingtv.Index.customView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.jh352160.surfingtv.Index.MainActivity;
import com.example.jh352160.surfingtv.Index.customView.adapter.IndexColumnAdapter;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.IndexItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jh352160 on 2016/7/21
 */

public class IndexColumn extends LinearLayout {

    Context context;

    public IndexColumn(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public IndexColumn(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.index_column, this);
        ListView listView = (ListView) findViewById(R.id.columnList);
        List<View> viewList = new ArrayList<>();
        int[] titleImag = {R.drawable.index_column_title1, R.drawable.index_column_title4, R.drawable.index_column_title2, R.drawable.index_column_title3};
        //int[] bigImage={R.drawable.index_column_big_image1,R.drawable.index_column_big_image2,R.drawable.index_column_big_image3,R.drawable.index_column_big_image4};
        String[] bigImage = new String[4];

        //api
        List<List<String>> nameLists = new ArrayList<>();
        List<List<String>> lineTagLists = new ArrayList<>();
        List<List<String>> priceLists = new ArrayList<>();
        List<List<String>> imageUrlLists = new ArrayList<>();
        List<List<String>> fromLists = new ArrayList<>();
        List<List<String>> detilUrlLists = new ArrayList<>();
        List<List<Boolean>> isLikeLists = new ArrayList<>();
        List<List<Integer>> targetIdLists = new ArrayList<>();

        List<IndexItem.DataBean.ProgramsBean> programsBeen = MainActivity.getData().getPrograms();
        for (int i = 0; i < titleImag.length; i++) {
            bigImage[i] = programsBeen.get(i).getProgram().getImageUrl();

            List<String> names = new ArrayList<>();
            List<String> lineTags = new ArrayList<>();
            List<String> prices = new ArrayList<>();
            List<String> imageUrls = new ArrayList<>();
            List<String> fromList = new ArrayList<>();
            List<String> detilUrlList = new ArrayList<>();
            List<Boolean> isLikeList = new ArrayList<>();
            List<Integer> targetIdList = new ArrayList<>();
            List<IndexItem.DataBean.ProgramsBean.ProductsBean> products = programsBeen.get(i).getProducts();
            for (int j = 0; j < products.size(); j++) {
                names.add(products.get(j).getName());
                lineTags.add(products.get(j).getSummary());
                prices.add("￥ " + products.get(j).getPrice());
                imageUrls.add(products.get(j).getImageUrl());
                fromList.add(products.get(j).getProductFrom());
                detilUrlList.add(products.get(j).getDetailUrl());
                isLikeList.add(products.get(j).getFavorite() == 1);
                targetIdList.add(products.get(j).getProductId());
            }
            nameLists.add(names);
            lineTagLists.add(lineTags);
            priceLists.add(prices);
            imageUrlLists.add(imageUrls);
            fromLists.add(fromList);
            detilUrlLists.add(detilUrlList);
            isLikeLists.add(isLikeList);
            targetIdLists.add(targetIdList);
        }

        for (int i = 0; i < titleImag.length; i++) {
            ItemIndexColumn item = new ItemIndexColumn(context, imageUrlLists.get(i), nameLists.get(i), lineTagLists.get(i), priceLists.get(i), fromLists.get(i), detilUrlLists.get(i), isLikeLists.get(i), targetIdLists.get(i));
            item.setImage(titleImag[i], bigImage[i], programsBeen.get(i).getProgram().getAdType(), programsBeen.get(i).getProgram().getContentId());
            //item.setDetailData(fromLists.get(i),detilUrlLists.get(i),isLikeLists.get(i));
            item.setAdapter();
            viewList.add(item);
        }
        IndexColumnAdapter adapter = new IndexColumnAdapter(context, R.layout.item_index_column, viewList);
        listView.setAdapter(adapter);
    }
}
